<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ccoupon extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
    function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
		$this->load->helper('url');
    	$this->load->database();
    	$this->load->model("centermod");
    	$this->auth();
    	
    	
	}
	
	function auth()
	{
		//$this->session->unset_userdata('user');
		if($this->session->userdata("user")==false)
		{
			header('Location:index.php?pages/loginadmin');
	
		}
			
	}//function 
	
	
	
	public function listcoupon()
	{
		$query=$this->db->get("coupon");
		
		$data["listcoupon"]=$query->result_array();
		$this->load->view("coupon/listcoupon",$data);
		
	}
	
	public function  createnewcoupon()
	{
		$this->load->view("coupon/coupon");
	}
	
	public function actioncreatecoupon()
	{
		$arr_coupon_info=$this->input->get_post("coupon");
		$arr_coupon_info["active"]=true;
		$this->db->insert('coupon', $arr_coupon_info);
		$this->listcoupon();
		
	}
	
	public function editcoupon()
	{
		$idcoupn=$this->input->get_post("idcoupn");
		$query=$this->db->get_where("coupon", array("idcoupn"=>$idcoupn));
		$data["couponinfo"]=$query->result_array();
		$data["edit"]=true;
		$this->load->view("coupon/coupon",$data);
		
	}
	
	public function desactivecoupon()
	{
		$idcoupn=$this->input->get_post("idcoupn");
		$arr_coupon_info["active"]=0;
		$this->db->where('idcoupn', $idcoupn);
		$this->db->update('coupon', $arr_coupon_info);
		$this->listcoupon();
	}
	
	
	
	public function actioneditcoupon()
	{
		$idcoupn=$this->input->get_post("idcoupn");
		$arr_coupon_info=$this->input->get_post("coupon");
		$arr_coupon_info["active"]=1;
		$this->db->where('idcoupn', $idcoupn);
		$this->db->update('coupon', $arr_coupon_info);
		$this->listcoupon();
	}
	
//cherche coupon from ajax

	public function getajaxcoupon()
	{
		$couponcode=$this->input->get_post("couponcode");
		$query=$this->db->get_where("coupon", array("coupon_code"=>$couponcode,"active"=>1));
		$coupon_arr=$query->result_array();
		$return_array=array();
		if(count($coupon_arr)>0)
		{
			$date_start=strtotime($coupon_arr[0]["coupon_start"]);
			$date_end=strtotime($coupon_arr[0]["coupon_end"]);
			$now=strtotime("now");
			if($date_start<=$now&&$now<=$date_end&&$coupon_arr[0]["active"]==1)
			{
				$return_array=$coupon_arr[0];
				$return_array["status"]="match";
				
			}else
			{
				$return_array["status"]="expried";
			}
			
		}else
		{
			$return_array["status"]="noexit";
		}
		echo  	json_encode($return_array);
	}
	
	
	
}
?>
